/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.ButtonGroup;
import com.sun.lwuit.CheckBox;
import com.sun.lwuit.Command;
import com.sun.lwuit.Font;
import com.sun.lwuit.Label;
import com.sun.lwuit.RadioButton;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BoxLayout;
import core.ScreenEngine;
import core.Settings;
import view.commands.BackCommand;

/**
 *
 * @author Kvest
 */
public class FontEditForm extends BasicForm
{
    private static final int ACCEPT_COMMAND_ID = 1;
    
    private RadioButton rbSmall;
    private RadioButton rbMedium;
    private RadioButton rbBig;
    private CheckBox cbBold;
    
    public FontEditForm()
    {
        super("font_edit");
        
        init();
    }
    
    private void init()
    {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        setCyclicFocus(false);
        
        addComponent(new Label("font_size"));   
        ButtonGroup group = new ButtonGroup();
        
        rbSmall = new RadioButton("small");
        rbSmall.setSelectCommandText("");
        group.add(rbSmall);
        addComponent(rbSmall);
        
        rbMedium = new RadioButton("medium");
        rbMedium.setSelectCommandText("");
        group.add(rbMedium);
        addComponent(rbMedium);
        
        rbBig = new RadioButton("big");
        rbBig.setSelectCommandText("");
        group.add(rbBig);
        addComponent(rbBig);
        
        cbBold = new CheckBox("bold");
        cbBold.setSelectCommandText("");
        addComponent(cbBold);
        
        addCommand(new BackCommand());
        
        Command accept = new Command("accept", ACCEPT_COMMAND_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {   
                //Сохряняем изменения
                accept();
                
                //Закрываем форму
                ScreenEngine.getEngine().pop(false);
                
                //Отображаем сообщение
                ScreenEngine.getEngine().showMessageBox("font_saved", (new Label("font_saved_text")).getText(), 20000); 
                
                evt.consume();
            }
        };
        addCommand(accept);
    }
    
    public void show()
    {
        setContent();
        
        super.show();
    }
   
    private void setContent()
    {
        Settings settings = Settings.getSettings();
        switch(settings.getFontSize())
        {
            case Font.SIZE_SMALL : rbSmall.setSelected(true); break;
            case Font.SIZE_MEDIUM : rbMedium.setSelected(true); break;
            case Font.SIZE_LARGE : rbBig.setSelected(true); break;
        }
        cbBold.setSelected(settings.isFontBold());
    }
    
    private void accept()
    {
        //Сохраняем выбранное
        Settings settings = Settings.getSettings();
        if (rbSmall.isSelected())
        {
            settings.setFontSize(Font.SIZE_SMALL);
        }
        else if (rbMedium.isSelected())
        {
            settings.setFontSize(Font.SIZE_MEDIUM);
        }
        else if (rbBig.isSelected())
        {
            settings.setFontSize(Font.SIZE_LARGE);
        }
        settings.setFontBold(cbBold.isSelected());
        settings.save();
    }
}
